<?php
	require_once 'includes/image_resizer.php';
?>


<?
if($sellflag!=1)
{
?>
<div id='panel'>
    <a name="wanted_list" id="wishlist"></a>
<div class="fulldiv" style="width:700px;">


 <div class="fulldiv" style="margin:10px 0px 0px 0px;">
 	<div style="float:left; margin:0px 10px 0px 10px; padding-top:8px;"><strong class="orangehead">My Wanted List</strong></div>
	<div class="greyarrowsml" onclick="show_hide_s_boxes('panel_mwishlist')" id="arrow_panel_mwishlist">&nbsp;</div>
	
	
	<div class="star" style="float: right; margin-top:11px;" id="star_panel_mwishlist">&nbsp;</div>
	<a href="javascript:void(0)" class="bluelink" style="float: right; margin-top:13px;" onclick="show_more_info5()" id="more_info_panel_mwishlist">more info</a>
	
	<script>
	function show_more_info5()
	{
	  var ntop = ( $(window).height() - $('#more_info5').height() ) / 2+$(window).scrollTop();
	  var nleft = ( $(window).width() - $('#more_info5').width() ) / 2+$(window).scrollLeft();
	
	  $('#more_info5').css({'left': nleft, 'top':ntop});
	  $('#more_info5').fadeIn('slow');
	}
	</script>
	<div style=" display: none; width: 400px; font-weight:bold; padding:10px; background-color:#cccccc; opacity:0.9;" class="user_popup" id="more_info5">
	  <span style="float:left; width:100%; text-align:right; margin-bottom:5px;"><label style="cursor:pointer;" onclick="$('#more_info5').fadeOut('slow');">x</label></span>
	  <span style="float:left; width:100%; margin-bottom:10px;" id="desc_info">Wanted Items are here to keep track of what you want to get in the future. Plus Wholesalers will make offers towards your specific wants and price to benefit you.</span>
	</div>
 </div>

<div id="panel_mwishlist">
<div class="fullbox_top">&nbsp;</div>
<div class="fullbox_repeat">

<table width="98%" align="center" border="0" cellpadding="0" cellspacing="0">
<?php /*?><tr><td class="contentHeader"><strong class="orangehead">My Wishlist</strong></td></tr><?php */?>
 <tr>
  <td>	
 <?php
	if(!empty($_POST['hidDelWishlist'])) {	
		$wId=$_POST['hidDelWishlist'];
		//get wishlist old images
		$sqlGetImage = "SELECT product_image, product_thumb FROM tbl_whole_wishlist  WHERE wishlist_id='$wId'";
		$rstGetImage = mysql_query($sqlGetImage) or die(mysql_error());
		$rowGetImage = mysql_fetch_array($rstGetImage);
		
		$sqlDelPro="Delete from tbl_whole_wishlist where wishlist_id=$wId";
		$rstDelPro=mysql_query($sqlDelPro);
		if($rstDelPro) { 			
			//delete old images
			if($rowGetImage[0]!="") { unlink($rowGetImage[0]); }
			if($rowGetImage[1]!="") { unlink($rowGetImage[1]); } 
			
			echo "<div id='suc-msg'> Item deleted from your wishlist. </div>";			
		}	
		else {
			echo "<div id='err-msg'> Error on deleting wishlist item. </div>";
		}
	}
	
	
	if(!empty($_POST['hidOrderWishlist'])) {
		$sqlSelLstOrder="SELECT * FROM tbl_whole_wishlist  where user_id='$userId'";
		$rstSelLstOrder=mysql_query($sqlSelLstOrder);
		$i=0;
		while($rowSelLstOrder=mysql_fetch_array($rstSelLstOrder))
		{
		$wishId=$rowSelLstOrder['wishlist_id'];
		$wishOrder=$_POST['wishOrder_'.$wishId.''];
		if(!empty($wishOrder) && !is_nan($wishOrder))
		{
		$sqlUpdWishList="UPDATE tbl_whole_wishlist  SET product_order=".$wishOrder." WHERE wishlist_id=$wishId";
		$rstUpdWishList=mysql_query($sqlUpdWishList);
		}
		$i++;
		}
		if($i>0){ 			
			echo "<div id='suc-msg'> Your wishlist order changed. </div>"; 
		}
		else {
			echo "<div id='err-msg'> Error on changing wishlist order. </div>";
		}
	}
	
	
	if(!empty($_POST['addWishListKey'])) {
		echo $productName=$_POST['productName'];
		$wishprice=$_POST['wishprice'];
		$productDescription=$_POST['productDescription'];
		$currentDate=date("Y-m-d");
		$sqlAddWishList="INSERT INTO tbl_whole_wishlist set user_id='$userId', wishlist_type=2, product_name='$productName', price='$wishprice', description='$productDescription',status=1, created_date='$currentDate'";
		$rstAddWishList=mysql_query($sqlAddWishList) or die(mysql_error());
		if(mysql_affected_rows()>0) {	
			$wishId=mysql_insert_id();
			$wishImage = $_FILES["productImage"]["name"];
			$imageInfo=pathinfo($wishImage);		
			$wishType = strtolower($imageInfo['extension']);
			if(!empty($wishType)) {						
				if($wishType=="jpeg" || $wishType=="jpg" || $wishType=="png" || $wishType=="gif") {				
					$rand=mt_rand();					
					$path="wishlist/source/".$rand.$wishId.".".$wishType;					
					$path_thumb="wishlist/thumb/".$rand.$wishId."_thumb.".$wishType;					
					$path_medium="wishlist/medium/".$rand.$wishId."_medium.".$wishType;									
					move_uploaded_file($_FILES["productImage"]["tmp_name"],$path);				  
					productResize($path, $path_thumb, $path_medium, $wishType);	
					
					$sql_updpro="UPDATE tbl_whole_wishlist set 
								 product_image='".$path_medium."',
								 product_thumb='".$path_thumb."'								
								 where wishlist_id=$wishId";	
					$rst_updpro=mysql_query($sql_updpro) or die(mysql_error());
					if(mysql_affected_rows()>0) { 
						unlink($path);
						echo "<div id='suc-msg'> Product added with image to your wishlist. </div>"; 
					} else {
						echo "<div id='suc-msg'> Product added to your wishlist. </div>						
							<div id='err-msg'> Error in adding product image.</div>";
					}	
				}else {
					echo "<div id='suc-msg'> Product added to your wishlist. </div>						
						<div id='err-msg'> Error in adding product image. Upload jpg,png and gif formats only.</div>";
				}	
			 } else {
			 	echo "<div id='suc-msg'> Product added to your wishlist. </div>";
			 }		
		}
		else {
			echo "<div id='err-msg'> Error in adding wishlist item, try again. </div>";
		}
		header("Location:index.php?mySwapen#wishlist");
		exit();
	}
		
		
	if(!empty($_POST['editWishListKey'])) {
		$wishListId=$_POST['editWishListKey'];
		
		//get wishlist old images
		$sqlGetImages = "SELECT product_image, product_thumb, wishlist_type FROM tbl_whole_wishlist WHERE wishlist_id='$wishListId'";
		$rstGetImages = mysql_query($sqlGetImages) or die(mysql_error());
		$rowGetImages = mysql_fetch_array($rstGetImages);
		
		//check wishlist type for updating...
		
		if($rowGetImages["wishlist_type"]=='2') {
		
				$productName=$_POST['productName'];
				$wishprice=$_POST['wishprice'];
				$productDescription=$_POST['productDescription'];
				$sqlUpdWishListProd="UPDATE tbl_whole_wishlist SET product_name='$productName', price='$wishprice', description='$productDescription' WHERE wishlist_id=$wishListId";
				$rstUpdWishListProd=mysql_query($sqlUpdWishListProd) or die(mysql_error());
				$noUpdWishListProd=mysql_affected_rows();
				if($noUpdWishListProd>0){ 			
					
					$wishId=$wishListId;
					
					$wishImage = $_FILES["productImage"]["name"];
					$imageInfo=pathinfo($wishImage);		
					$wishType = strtolower($imageInfo['extension']);
					if(!empty($wishType)) {						
						if($wishType=="jpeg" || $wishType=="jpg" || $wishType=="png" || $wishType=="gif") {				
							$rand=mt_rand();					
							$path="wishlist/source/".$rand.$wishId.".".$wishType;					
							$path_thumb="wishlist/thumb/".$rand.$wishId."_thumb.".$wishType;					
							$path_medium="wishlist/medium/".$rand.$wishId."_medium.".$wishType;									
							move_uploaded_file($_FILES["productImage"]["tmp_name"],$path);				  
							productResize($path, $path_thumb, $path_medium, $wishType);	
							
							$sql_updpro="UPDATE tbl_whole_wishlist  set 
										 product_image='".$path_medium."',
										 product_thumb='".$path_thumb."'								
										 where wishlist_id=$wishId";	
							$rst_updpro=mysql_query($sql_updpro) or die(mysql_error());
							if(mysql_affected_rows()>0) { 
								unlink($path);
								
								//delete old images
								if($rowGetImages[0]!="") { unlink($rowGetImages[0]); }
								if($rowGetImages[1]!="") { unlink($rowGetImages[1]); }
								
								echo "<div id='suc-msg'> Product updated with product image to your wishlist. </div>"; 
							} else {
								echo "<div id='suc-msg'> Wishlist product updated to your wishlist. </div>						
									<div id='err-msg'> Error in updating product image.</div>";
							}	
						}else {
							echo "<div id='suc-msg'> Wishlist product updated. </div>						
								<div id='err-msg'> Error in updating product image. Upload jpg,png and gif formats only.</div>";
						}	
					 } else {
						echo "<div id='suc-msg'> Wishlist product updated. </div>";
					 }		
				}
				else {
					echo "<div id='err-msg'> Error on updating wishlist product. </div>";
				}
		} // end of checking if wishlist type own
		 
		else {
				// otherwise wishlist should be added one....
				$wishprice=$_POST['wishprice'];				
				$sqlUpdWishListProd="UPDATE tbl_whole_wishlist SET price='$wishprice' WHERE wishlist_id=$wishListId";
				$rstUpdWishListProd=mysql_query($sqlUpdWishListProd) or die(mysql_error());				
				if(mysql_affected_rows()>0){
					echo "<div id='suc-msg'> Wishlist price updated. </div>";
				} 
		
		}		
	}						   					   
				   
		 ?>
		 <form name="frmWishlist" id="frmWishlist" method="post" action="">
		 <input type="hidden" name="hidDelWishlist" id="hidDelWishlist">
         <input type="hidden" name="hidOrderWishlist" id="hidOrderWishlist">
         <input type="hidden" name="hidEditWishlist" id="hidEditWishlist">		 
		 <?php		   
				   $sqlSelLst="SELECT * FROM tbl_whole_wishlist  where user_id='$userId' ORDER BY product_order LIMIT 0,10";
				   $rstSelLst=mysql_query($sqlSelLst);				  
				   $num=mysql_num_rows($rstSelLst);
				  
				   echo "<table width='100%' border='0' align='center' cellspacing='0'>";
				    
				   if($num==0)
					{
                                   
                                                
					  echo '<tr><td colspan="6" align="center">
                                                    <div class="box" style="float:none;">
                                                        <div class="box_top">&nbsp;</div>
                                                        <div class="box_middle">
                                                            <div class="boxtxt_out"><strong>Are there products you\'ve always wanted but couldnt find for the right price?</strong></div>
                                                            <div class="boxtxt_out"><div class="box_img"><img style="cursor:pointer;" src="images/starthere_blue.png" onClick="showAddWishlist(0,\'divWishlistAction\')" /></div></div>
                                                        </div>
                                                        <div class="box_bottom">&nbsp;</div>
                                                    </div>
                                                    </td>
                                                </tr>';  
					}
				   else {
                                    echo "<tr class='head-tr'>
						  <td width='3%'>#</td>
						  <td width='8%'><strong>&nbsp; </strong></td>									 
						  <td width='40%'><strong>Product Name</strong></td>						  
						  <td width='20%'><strong>Wanted Price</strong> </td>						 
						  <td width='20%'>&nbsp; </td>	
						</tr>";
				   		 $q=1; 		 
						 while($rowSelLst=mysql_fetch_array($rstSelLst))
					     {
						   $wishId=$rowSelLst['wishlist_id'];
						   $itemId = $rowSelLst['item_id'];
						   $wishlistType=$rowSelLst['wishlist_type'];
						   $productPrice = $rowSelLst['price'];						  
						   $productName=wordFormater($rowSelLst['product_name'],30);
						   
						   if($wishlistType=='2') {
						   		$proLink = "wwishproduct=".$wishId;
						   } else {
						   		$proLink = "wholeproducts=".$itemId;
						   }
						   							
						   echo "<tr>
						        <td height='28'> $q </td>
								<td><!--<input name='wishOrder_".$wishId."' id='wishOrder_".$wishId."' type='text' size='1' class='inputbg'/>--></td>
								<td><a href='index.php?$proLink' title='$productName'>$productName</a></td>								 
								<td> $".number_format($productPrice,2)."</td>								
								<td><a href='javascript:void(0);' onClick='funEditWWishlistForm($wishId,\"divWishlistAction\")'>Edit</a> | <a href='javascript:void(0);' onClick='funDelWishlist($wishId)'>Delete </a> </td>
						       </tr>";							
						 
   			               echo "<tr>
									 <td colspan='5'> <div style='border-top:1px solid #CCCCCC;'> </div> </td>
								  </tr>";
						 $q++;
						 }
					echo "<tr>
								 
								 <td colspan='5'><input type='button' name='addProduct' id='addProduct' value='Add to List'  onClick='showAddWishlist(0,\"divWishlistAction\")' class='sbmt_btn'><br><br></td>
							  </tr>";	 				    	
					} 		
						
					echo "</table>";		
	?>
    <input type="hidden" name="hidWishlistType" id="hidWishlistType" value="2">		
	</form>
	<div id="divWishlistAction">  </div>  
  </td>
 </tr>
</table>

</div>
<div class="fullbox_bot">&nbsp;</div>
</div>
<hr style="float:left; width:500px; display:none;" id="hr_line_panel_mwishlist" />

</div>
</div>
<?
}
?>
<br>
	<?php
	if(isset($_GET['ord']))
	{
		//when user is clicking on sort in transaction history 
		?>
		<script>
		   $(document).ready(function() { 
			only_open('tran_history');
			only_open('your_inventory');
			only_open('comm_pay');
			only_open('post_listing');
			only_open('classifieds1');
			only_open('myswapen_refcomm');
			only_open('feedback_pending_merc');
			only_open('panel_mwishlist');
		    });
		</script>
		<?php
	}
	else if(isset($_GET['page']))
	{
		//when user is clicking on pagination in commission i owe
		?>
		<script>
		   $(document).ready(function() { 
			only_close('tran_history');
			only_close('your_inventory');
			only_open('comm_pay');
			only_close('post_listing');
			only_close('classifieds1');
			only_close('myswapen_refcomm');
			only_close('feedback_pending_merc');
			only_close('panel_mwishlist');
		    });
		</script>
		<?php
	}
	else {
	?>
		<script>
		   $(document).ready(function() {
			defaultset('merchant',<?php echo $numload?>);
		    });
		</script>
	<?php
	}
	?>

<?php /*?>------------buttons---------------
<tr>
								 <td>&nbsp;</td>
								 <td colspan='4'><input type='button' name='addProduct' id='addProduct' value='Add More +'  onClick='showAddWishlist(0,\"divWishlistAction\")' class='sbmt_btn'>&nbsp;&nbsp; <input type='button' name='changeOrder' id='changeOrder' value='Change Order' onClick='funEditWishOrder()' class='sbmt_btn'><br><br></td>
							  </tr>
							  
		------------buttons---------------	  <?php */?>